/*
 * Created by 2020-06-25 13:44:36
 */
package com.agg.persist.xml.loader;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

public abstract class AbstractResourceLoader implements  ResourceLoader {
	private  Filter filter = null;

	/**
	 * @return the file filter, default the xml file filter.
	 */
	@Override
	public  Filter getFilter() {
		if(filter==null){
			filter = new  Filter(){
				public boolean isSatisfied(String fileName) {
					if(fileName.endsWith(".xml")||fileName.endsWith(".XML")){return true;}
					else {return false;}
				}};
		}
		return filter;
	}

	/**
	 * @param filter
	 */
	@Override
	public void setFilter(Filter filter) {
		this.filter = filter;
	}

	@Override
	public boolean loadResource(ResourceCallBack callback, String  path) throws IOException {
		boolean success = true;
		//根据路径通配符加载配置
		ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
		Resource[] sources = resourceLoader.getResources(path);
		for(Resource resource: sources){
			if(!loadResource(callback,resource)) success=false;
		}
		return success;
	}
}
